home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 2.iso / STUTTGART / LANG / C / GCC / V2-4-5 / GPPLIBSR00 / cc / editbuf next >
Text File  |  1993-12-08  |  19KB  |  709 lines

  1. //    This is part of the iostream library, providing input/output for C++.
  2. //    Copyright (C) 1991 Per Bothner.
  3. //
  4. //    This library is free software; you can redistribute it and/or
  5. //    modify it under the terms of the GNU Library General Public
  6. //    License as published by the Free Software Foundation; either
  7. //    version 2 of the License, or (at your option) any later version.
  8. //
  9. //    This library is distributed in the hope that it will be useful,
  10. //    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  12. //    Library General Public License for more details.
  13. //
  14. //    You should have received a copy of the GNU Library General Public
  15. //    License along with this library; if not, write to the Free
  16. //    Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17.  
  18. #include "ioprivate.h"
  19. extern "C" { #include <stddef.h> }
  20.  
  21. #ifdef __GNUG__
  22. #pragma implementation "editbuf.h"
  23. #endif
  24. #include "editbuf.h"
  25.  
  26. /* NOTE: Some of the code here is taken from GNU emacs */
  27. /* Hence this file falls under the GNU License! */
  28.  
  29. // Invariants for edit_streambuf:
  30. // An edit_streambuf is associated with a specific edit_string,
  31. // which again is a sub-string of a specific edit_buffer.
  32. // An edit_streambuf is always in either get mode or put mode, never both.
  33. // In get mode, gptr() is the current position,
  34. // and pbase(), pptr(), and epptr() are all NULL.
  35. // In put mode, pptr() is the current position,
  36. // and eback(), gptr(), and egptr() are all NULL.
  37. // Any edit_streambuf that is actively doing insertion (as opposed to
  38. // replacing) // must have its pptr() pointing to the start of the gap.
  39. // Only one edit_streambuf can be actively inserting into a specific
  40. // edit_buffer; the edit_buffer's _writer field points to that edit_streambuf.
  41. // That edit_streambuf "owns" the gap, and the actual start of the
  42. // gap is the pptr() of the edit_streambuf; the edit_buffer::_gap_start pointer
  43. // will only be updated on an edit_streambuf::overflow().
  44.  
  45. int edit_streambuf::truncate()
  46. {
  47.     str->buffer->delete_range(str->buffer->tell((buf_char*)pptr()),
  48.                   str->buffer->tell(str->end));
  49.     return 0;
  50. }
  51.  
  52. #ifdef OLD_STDIO
  53. inline void  disconnect_gap_from_file(edit_buffer* buffer, FILE* fp)
  54. {
  55.     if (buffer->gap_start_ptr != &fp->__bufp)
  56.     return;
  57.     buffer->gap_start_normal = fp->__bufp;
  58.     buffer->gap_start_ptr = &buffer->gap_start_normal;
  59. }
  60. #endif
  61.  
  62. void edit_streambuf::flush_to_buffer(edit_buffer* buffer)
  63. {
  64.     if (pptr() > buffer->_gap_start && pptr() < buffer->gap_end())
  65.     buffer->_gap_start = pptr();
  66. }
  67.  
  68. void edit_streambuf::disconnect_gap_from_file(edit_buffer* buffer)
  69. {
  70.     if (buffer->_writer != this) return;
  71.     flush_to_buffer(buffer);
  72.     setp(pptr(),pptr());
  73.     buffer->_writer = NULL;    
  74. }
  75.  
  76. buf_index edit_buffer::tell(buf_char *ptr)
  77. {
  78.     if (ptr <= gap_start())
  79.     return ptr - data;
  80.     else
  81.     return ptr - gap_end() + size1();
  82. }
  83.  
  84. #if 0
  85. buf_index buf_cookie::tell()
  86. {
  87.     return str->buffer->tell(file->__bufp);
  88. }
  89. #endif
  90.  
  91. buf_index edit_buffer::tell(edit_mark*mark)
  92. {
  93.     return tell(data + mark->index_in_buffer(this));
  94. }
  95.  
  96. // adjust the position of the gap
  97.  
  98. void edit_buffer::move_gap(buf_offset pos)
  99. {
  100.   if (pos < size1())
  101.     gap_left (pos);
  102.   else if (pos > size1())
  103.     gap_right (pos);
  104. }
  105.  
  106. void edit_buffer::gap_left (int pos)
  107. {
  108.   register buf_char *to, *from;
  109.   register int i;
  110.   int new_s1;
  111.  
  112.   i = size1();
  113.   from = gap_start();
  114.   to = from + gap_size();
  115.   new_s1 = size1();
  116.  
  117.   /* Now copy the characters.  To move the gap down,
  118.      copy characters up.  */
  119.  
  120.   for (;;)
  121.     {
  122.       /* I gets number of characters left to copy.  */
  123.       i = new_s1 - pos;
  124.       if (i == 0)
  125.     break;
  126. #if 0
  127.       /* If a quit is requested, stop copying now.
  128.      Change POS to be where we have actually moved the gap to.  */
  129.       if (QUITP)
  130.     {
  131.       pos = new_s1;
  132.       break;
  133.     }
  134. #endif
  135.       /* Move at most 32000 chars before checking again for a quit.  */
  136.       if (i > 32000)
  137.     i = 32000;
  138.       new_s1 -= i;
  139.       while (--i >= 0)
  140.     *--to = *--from;
  141.     }
  142.  
  143.   /* Adjust markers, and buffer data structure, to put the gap at POS.
  144.      POS is where the loop above stopped, which may be what was specified
  145.      or may be where a quit was detected.  */
  146.   adjust_markers (pos << 1, size1() << 1, gap_size(), data);
  147. #ifndef OLD_STDIO
  148.   _gap_start = data + pos;
  149. #else
  150.   if (gap_start_ptr == &gap_start_normal)
  151.     gap_start_normal = data + pos;
  152. #endif
  153.   __gap_end_pos = to - data;
  154. /*  QUIT;*/
  155. }
  156.  
  157. void edit_buffer::gap_right (int pos)
  158. {
  159.   register buf_char *to, *from;
  160.   register int i;
  161.   int new_s1;
  162.  
  163.   i = size1();
  164.   to = gap_start();
  165.   from = i + gap_end();
  166.   new_s1 = i;
  167.  
  168.   /* Now copy the characters.  To move the gap up,
  169.      copy characters down.  */
  170.  
  171.   while (1)
  172.     {
  173.       /* I gets number of characters left to copy.  */
  174.       i = pos - new_s1;
  175.       if (i == 0)
  176.     break;
  177. #if 0
  178.       /* If a quit is requested, stop copying now.
  179.      Change POS to be where we have actually moved the gap to.  */
  180.       if (QUITP)
  181.     {
  182.       pos = new_s1;
  183.       break;
  184.     }
  185. #endif
  186.       /* Move at most 32000 chars before checking again for a quit.  */
  187.       if (i > 32000)
  188.     i = 32000;
  189.       new_s1 += i;
  190.       while (--i >= 0)
  191.     *to++ = *from++;
  192.     }
  193.  
  194.   adjust_markers ((size1() + gap_size()) << 1, (pos + gap_size()) << 1,
  195.     - gap_size(), data);
  196. #ifndef OLD_STDIO
  197.   _gap_start = data+pos;
  198. #else
  199.   if (gap_start_ptr == &gap_start_normal)
  200.     gap_start_normal = data + pos;
  201. #endif
  202.   __gap_end_pos = from - data;
  203. /*  QUIT;*/
  204. }
  205.  
  206. /* make sure that the gap in the current buffer is at least k
  207.    characters wide */
  208.  
  209. void edit_buffer::make_gap(buf_offset k)
  210. {
  211.   register buf_char *p1, *p2, *lim;
  212.   buf_char *old_data = data;
  213.   int s1 = size1();
  214.  
  215.   if (gap_size() >= k)
  216.     return;
  217.  
  218.   /* Get more than just enough */
  219.   if (buf_size > 1000) k += 2000;
  220.   else k += /*200;*/ 20; // for testing!
  221.  
  222.   p1 = (buf_char *) realloc (data, s1 + size2() + k);
  223.   if (p1 == 0)
  224.     abort(); /*memory_full ();*/
  225.  
  226.   k -= gap_size();            /* Amount of increase.  */
  227.  
  228.   /* Record new location of text */
  229.   data = p1;
  230.  
  231.   /* Transfer the new free space from the end to the gap
  232.      by shifting the second segment upward */
  233.   p2 = data + buf_size;
  234.   p1 = p2 + k;
  235.   lim = p2 - size2();
  236.   while (lim < p2)
  237.     *--p1 = *--p2;
  238.  
  239.   /* Finish updating text location data */
  240.   __gap_end_pos += k;
  241.  
  242. #ifndef OLD_STDIO
  243.   _gap_start = data + s1;
  244. #else
  245.   if (gap_start_ptr == &gap_start_normal)
  246.     gap_start_normal = data + s1;
  247. #endif
  248.  
  249.   /* adjust markers */
  250.   adjust_markers (s1 << 1, (buf_size << 1) + 1, k, old_data);
  251.   buf_size += k;
  252. }
  253.  
  254. /* Add `amount' to the position of every marker in the current buffer
  255.    whose current position is between `from' (exclusive) and `to' (inclusive).
  256.    Also, any markers past the outside of that interval, in the direction
  257.    of adjustment, are first moved back to the near end of the interval
  258.    and then adjusted by `amount'.  */
  259.  
  260. void edit_buffer::adjust_markers(register mark_pointer low,
  261.                  register mark_pointer high,
  262.                  int amount, buf_char *old_data)
  263. {
  264.   register struct edit_mark *m;
  265.   register mark_pointer mpos;
  266.   /* convert to mark_pointer */
  267.   amount <<= 1;
  268.  
  269.   if (_writer)
  270.       _writer->disconnect_gap_from_file(this);
  271.  
  272.   for (m = mark_list(); m != NULL; m = m->chain)
  273.     {
  274.       mpos = m->_pos;
  275.       if (amount > 0)
  276.     {
  277.       if (mpos > high && mpos < high + amount)
  278.         mpos = high + amount;
  279.     }
  280.       else
  281.     {
  282.       if (mpos > low + amount && mpos <= low)
  283.         mpos = low + amount;
  284.     }
  285.       if (mpos > low && mpos <= high)
  286.     mpos += amount;
  287.       m->_pos = mpos;
  288.     }
  289.  
  290.     // Now adjust files
  291.     edit_streambuf *file;
  292.  
  293.     for (file = files; file != NULL; file = file->next) {
  294.     mpos = file->current() - old_data;
  295.     if (amount > 0)
  296.     {
  297.       if (mpos > high && mpos < high + amount)
  298.         mpos = high + amount;
  299.     }
  300.     else
  301.     {
  302.       if (mpos > low + amount && mpos <= low)
  303.         mpos = low + amount;
  304.     }
  305.     if (mpos > low && mpos <= high)
  306.         mpos += amount;
  307.     char* new_pos = data + mpos;
  308.     file->set_current(new_pos, file->is_reading());
  309.     }
  310. }
  311.  
  312. #if 0
  313. stdio_
  314.    __off == index at start of buffer (need only be valid after seek ? )
  315.    __buf ==
  316.  
  317. if read/read_delete/overwrite mode:
  318.      __endp <= min(*gap_start_ptr, edit_string->end->ptr(buffer))
  319.  
  320. if inserting:
  321.      must have *gap_start_ptr == __bufp && *gap_start_ptr+gap == __endp
  322.      file->edit_string->end->ptr(buffer) == *gap_start_ptr+end
  323. if write_mode:
  324.      if before gap
  325. #endif
  326.  
  327. int edit_streambuf::underflow()
  328. {
  329.     if (!(_mode & ios::in))
  330.     return EOF;
  331.     struct edit_buffer *buffer = str->buffer;
  332.     if (!is_reading()) { // Must switch from put to get mode.
  333.     disconnect_gap_from_file(buffer);
  334.     set_current(pptr(), 1);
  335.     }
  336.     buf_char *str_end = str->end->ptr(buffer);
  337.   retry:
  338.     if (gptr() < egptr()) {
  339.     return *gptr();
  340.     }
  341.     if ((buf_char*)gptr() == str_end)
  342.     return EOF;
  343.     if (str_end <= buffer->gap_start()) {
  344.     setg(eback(), gptr(), str_end);
  345.     goto retry;
  346.     }
  347.     if (gptr() < buffer->gap_start()) {
  348.     setg(eback(), gptr(), buffer->gap_start());
  349.     goto retry;
  350.     }
  351.     if (gptr() == buffer->gap_start()) {
  352.     disconnect_gap_from_file(buffer);
  353. //    fp->__offset += fp->__bufp - fp->__buffer;
  354.     setg(buffer->gap_end(), buffer->gap_end(), str_end);
  355.     }
  356.     else
  357.     setg(eback(), gptr(), str_end);
  358.     goto retry;
  359. }
  360.  
  361. int edit_streambuf::overflow(int ch)
  362. {
  363.     if (_mode == ios::in)
  364.     return EOF;
  365.     struct edit_buffer *buffer = str->buffer;
  366.     flush_to_buffer(buffer);
  367.     if (ch == EOF)
  368.     return 0;
  369.     if (is_reading()) { // Must switch from get to put mode.
  370.     set_current(gptr(), 0);
  371.     }
  372.     buf_char *str_end = str->end->ptr(buffer);
  373.   retry:
  374.     if (pptr() < epptr()) {
  375.     *pptr() = ch;
  376.     pbump(1);
  377.     return (unsigned char)ch;
  378.     }
  379.     if ((buf_char*)pptr() == str_end || inserting()) {
  380.     /* insert instead */
  381.     if (buffer->_writer)
  382.         buffer->_writer->flush_to_buffer(); // Redundant?
  383.     buffer->_writer = NULL;
  384.     if  (pptr() >= buffer->gap_end())
  385.         buffer->move_gap(pptr() - buffer->gap_size());
  386.     else
  387.         buffer->move_gap(pptr());
  388.     buffer->make_gap(1);
  389.     setp(buffer->gap_start(), buffer->gap_end());
  390.     buffer->_writer = this;
  391.     *pptr() = ch;
  392.     pbump(1);
  393.     return (unsigned char)ch;
  394.     }
  395.     if (str_end <= buffer->gap_start()) {
  396.     // Entire string is left of gap.
  397.     setp(pptr(), str_end);
  398.     }
  399.     else if (pptr() < buffer->gap_start()) {
  400.     // Current pos is left of gap.
  401.     setp(pptr(), buffer->gap_start());
  402.     goto retry;
  403.     }
  404.     else if (pptr() == buffer->gap_start()) {
  405.     // Current pos is at start of gap; move to end of gap.
  406. //    disconnect_gap_from_file(buffer);
  407.     setp(buffer->gap_end(), str_end);
  408. //    __offset += __bufp - __buffer;
  409.     }
  410.     else {
  411.     // Otherwise, current pos is right of gap.
  412.     setp(pptr(), str_end);
  413.     }
  414.     goto retry;
  415. }
  416.  
  417. void edit_streambuf::set_current(char *new_pos, int reading)
  418. {
  419.     if (reading) {
  420.     setg(new_pos, new_pos, new_pos);
  421.     setp(NULL, NULL);
  422.     }
  423.     else {
  424.     setg(NULL, NULL, NULL);
  425.     setp(new_pos, new_pos);
  426.     }
  427. }
  428.  
  429. // Called by fseek(fp, pos, whence) if fp is bound to a edit_buffer.
  430.  
  431. streampos edit_streambuf::seekoff(streamoff offset, _seek_dir dir,
  432.                   int mode /* =ios::in|ios::out*/)
  433. {
  434.     struct edit_buffer *buffer = str->buffer;
  435.     disconnect_gap_from_file(buffer);
  436.     buf_index cur_pos = buffer->tell((buf_char*)current());;
  437.     buf_index start_pos = buffer->tell(str->start);
  438.     buf_index end_pos = buffer->tell(str->end);
  439.     switch (dir) {
  440.       case ios::beg:
  441.     offset += start_pos;
  442.     break;
  443.       case ios::cur:
  444.     offset += cur_pos;
  445.     break;
  446.       case ios::end:
  447.     offset += end_pos;
  448.     break;
  449.     }
  450.     if (offset < start_pos || offset > end_pos)
  451.     return EOF;
  452.     buf_char *new_pos = buffer->data + offset;
  453.     buf_char* gap_start = buffer->gap_start();
  454.     if (new_pos > gap_start) {
  455.     buf_char* gap_end = buffer->gap_end();
  456.     new_pos += gap_end - gap_start;
  457.     if (new_pos >= buffer->data + buffer->buf_size) abort(); // Paranoia.
  458.     }
  459.     set_current(new_pos, is_reading());
  460.     return EOF;
  461. }
  462.  
  463. #if 0
  464. int buf_seek(void *arg_cookie, fpos_t * pos, int whence)
  465. {
  466.     struct buf_cookie *cookie = arg_cookie;
  467.     FILE *file = cookie->file;
  468.     struct edit_buffer *buffer = cookie->str->buffer;
  469.     buf_char *str_start = cookie->str->start->ptr(buffer);
  470.     disconnect_gap_from_file(buffer, cookie->file);
  471.     fpos_t cur_pos, new_pos;
  472.     if (file->__bufp <= *buffer->gap_start_ptr
  473.     || str_start >= buffer->__gap_end)
  474.     cur_pos = str_start - file->__bufp;
  475.     else
  476.     cur_pos =
  477.         (*buffer->gap_start_ptr - str_start) + (file->__bufp - __gap_end);
  478.     end_pos = ...;
  479.     switch (whence) {
  480.       case SEEK_SET:
  481.     new_pos = *pos;
  482.     break;
  483.       case SEEK_CUR:
  484.     new_pos = cur_pos + *pos;
  485.     break;
  486.       case SEEK_END:
  487.     new_pos = end_pos + *pos;
  488.     break;
  489.     }
  490.     if (new_pos > end_pos) {
  491.     seek to end_pos;
  492.     insert_nulls(new_pos - end_pos);
  493.     return;
  494.     }
  495.     if (str_start + new_pos <= *gap_start_ptr &* *gap_start_ptr < end) {
  496.     __buffer = str_start;
  497.         __off = 0;
  498.     __bufp = str_start + new_pos;
  499.     file->__get_limit =
  500.         *buffer->gap_start_ptr; /* what if gap_start_ptr == &bufp ??? */
  501.     } else if () {
  502.     
  503.     }
  504.     *pos = new_pos;
  505. }
  506. #endif
  507.  
  508. /* Delete characters from `from' up to (but not incl) `to' */
  509.  
  510. void edit_buffer::delete_range (buf_index from, buf_index to)
  511. {
  512.   register int numdel;
  513.  
  514.   if ((numdel = to - from) <= 0)
  515.     return;
  516.  
  517.   /* Make sure the gap is somewhere in or next to what we are deleting */
  518.   if (from > size1())
  519.     gap_right (from);
  520.   if (to < size1())
  521.     gap_left (to);
  522.  
  523.   /* Relocate all markers pointing into the new, larger gap
  524.      to point at the end of the text before the gap.  */
  525.   adjust_markers ((to + gap_size()) << 1, (to + gap_size()) << 1,
  526.     - numdel - gap_size(), data);
  527.  
  528.    __gap_end_pos = to + gap_size();
  529.   _gap_start = data + from;
  530. }
  531.  
  532. void edit_buffer::delete_range(struct edit_mark *start, struct edit_mark *end)
  533. {
  534.     delete_range(tell(start), tell(end));
  535. }
  536.  
  537. void buf_delete_chars(struct edit_buffer *buf, struct edit_mark *mark, size_t count)
  538. {
  539.  abort();
  540. }
  541.  
  542. edit_streambuf::edit_streambuf(edit_string* bstr, int mode)
  543. {
  544.     _mode = mode;
  545.     str = bstr;
  546.     edit_buffer* buffer = bstr->buffer;
  547.     next = buffer->files;
  548.     buffer->files = this;
  549.     char* buf_ptr = bstr->start->ptr(buffer);
  550.     _inserting = 0;
  551. //    setb(buf_ptr, buf_ptr, 0);
  552.     set_current(buf_ptr, !(mode & ios::out+ios::trunc+ios::app));
  553.     if (_mode & ios::trunc)
  554.     truncate();
  555.     if (_mode & ios::ate)
  556.     seekoff(0, ios::end);
  557. }
  558.  
  559. // Called by fclose(fp) if fp is bound to a edit_buffer.
  560.  
  561. #if 0
  562. static int buf_close(void *arg)
  563. {
  564.     register struct buf_cookie *cookie = arg;
  565.     struct edit_buffer *buffer = cookie->str->buffer;
  566.     struct buf_cookie **ptr;
  567.     for (ptr = &buffer->files; *ptr != cookie; ptr = &(*ptr)->next) ;
  568.     *ptr = cookie->next;
  569.     disconnect_gap_from_file(buffer, cookie->file);
  570.     free (cookie);
  571.     return 0;
  572. }
  573. #endif
  574.  
  575. edit_streambuf::~edit_streambuf()
  576. {
  577.     if (_mode == ios::out)
  578.     truncate();
  579.     // Unlink this from list of files associated with bstr->buffer.
  580.     edit_streambuf **ptr = &str->buffer->files;
  581.     for (; *ptr != this; ptr = &(*ptr)->next) { }
  582.     *ptr = next;
  583.  
  584.     disconnect_gap_from_file(str->buffer);
  585. }
  586.  
  587. edit_buffer::edit_buffer()
  588. {
  589.     buf_size = /*200;*/ 15; /* for testing! */
  590.     data = (buf_char*)malloc(buf_size);
  591.     files = NULL;
  592. #ifndef OLD_STDIO
  593.     _gap_start = data;
  594.     _writer = NULL;
  595. #else
  596.     gap_start_normal = data;
  597.     gap_start_ptr = &gap_start_normal;
  598. #endif
  599.     __gap_end_pos = buf_size;
  600.     start_mark.chain = &end_mark;
  601.     start_mark._pos = 0;
  602.     end_mark.chain = NULL;
  603.     end_mark._pos = 2 * buf_size + 1;
  604. }
  605.  
  606. // Allocate a new mark, which is adjusted by 'delta' bytes from 'this'.
  607. // Restrict new mark to lie within 'str'.
  608.  
  609. edit_mark::edit_mark(struct edit_string *str, long delta)
  610. {
  611.     struct edit_buffer *buf = str->buffer;
  612.     chain = buf->start_mark.chain;
  613.     buf->start_mark.chain = this;
  614.     mark_pointer size1 = buf->size1() << 1;
  615.     int gap_size = buf->gap_size() << 1;
  616.     delta <<= 1;
  617.  
  618.     // check if new and old marks are opposite sides of gap
  619.     if (_pos <= size1 && _pos + delta > size1)
  620.     delta += gap_size;
  621.     else if (_pos >= size1 + gap_size && _pos + delta < size1 + gap_size)
  622.     delta -= gap_size;
  623.  
  624.     _pos = _pos + delta;
  625.     if (_pos < str->start->_pos & ~1)
  626.     _pos = (str->start->_pos & ~ 1) + (_pos & 1);
  627.     else if (_pos >= str->end->_pos)
  628.     _pos = (str->end->_pos & ~ 1) + (_pos & 1);
  629. }
  630.  
  631. // A (slow) way to find the buffer a mark belongs to.
  632.  
  633. edit_buffer * edit_mark::buffer()
  634. {
  635.     struct edit_mark *mark;
  636.     for (mark = this; mark->chain != NULL; mark = mark->chain) ;
  637.     // Assume that the last mark on the chain is the end_mark.
  638.     return (edit_buffer *)((char*)mark - offsetof(edit_buffer, end_mark));
  639. }
  640.  
  641. edit_mark::~edit_mark()
  642. {
  643.     // Must unlink mark from chain of owning buffer
  644.     struct edit_buffer *buf = buffer();
  645.     if (this == &buf->start_mark || this == &buf->end_mark) abort();
  646.     edit_mark **ptr;
  647.     for (ptr = &buf->start_mark.chain; *ptr != this; ptr = &(*ptr)->chain) ;
  648.     *ptr = this->chain;
  649. }
  650.  
  651. int edit_string::length() const
  652. {
  653.     ptrdiff_t delta = end->ptr(buffer) - start->ptr(buffer);
  654.     if (end->ptr(buffer) <= buffer->gap_start() ||
  655.     start->ptr(buffer) >= buffer->gap_end())
  656.     return delta;
  657.     return delta - buffer->gap_size();
  658. }
  659.  
  660. buf_char * edit_string::copy_bytes(int *lenp) const
  661. {
  662.     char *new_str;
  663.     int len1, len2;
  664.     buf_char *start1, *start2;
  665.     start1 = start->ptr(buffer);
  666.     if (end->ptr(buffer) <= buffer->gap_start()
  667.     || start->ptr(buffer) >= buffer->gap_end()) {
  668.     len1 = end->ptr(buffer) - start1;
  669.     len2 = 0;
  670.     start2 = NULL; // To avoid a warning from g++.
  671.     }
  672.     else {
  673.     len1 = buffer->gap_start() - start1;
  674.     start2 = buffer->gap_end();
  675.     len2 = end->ptr(buffer) - start2;
  676.     }
  677.     new_str = (char*)malloc(len1 + len2 + 1);
  678.     memcpy(new_str, start1, len1);
  679.     if (len2 > 0) memcpy(new_str + len1, start2, len2);
  680.     new_str[len1+len2] = '\0';
  681.     *lenp = len1+len2;
  682.     return new_str;
  683. }
  684.  
  685. // Replace the buf_chars in 'this' with ones from 'src'.
  686. // Equivalent to deleting this, then inserting src, except tries
  687. // to leave marks in place: Marks whose offset from the start
  688. // of 'this' is less than 'src->length()' will still have the
  689. // same offset in 'this' when done.
  690.  
  691. void edit_string::assign(struct edit_string *src)
  692. {
  693.     edit_streambuf dst_file(this, ios::out);
  694.     if (buffer == src->buffer /*&& ???*/) { /* overly conservative */
  695.     int src_len;
  696.     buf_char *new_str;
  697.     new_str = src->copy_bytes(&src_len);
  698.     dst_file.sputn(new_str, src_len);
  699.     free (new_str);
  700.     } else {
  701.     edit_streambuf src_file(src, ios::in);
  702.     for ( ; ; ) {
  703.         int ch = src_file.sbumpc();
  704.         if (ch == EOF) break;
  705.         dst_file.sputc(ch);
  706.     }
  707.     }
  708. }
  709.